<?php
	define ("MAX_SIZE","4096");
	function getExtension($str)
	{
		 $i = strrpos($str,".");
		 if (!$i) { return ""; }
		 $l = strlen($str) - $i;
		 $ext = substr($str,$i+1,$l);
		 return $ext;
	}
?>

<?php

		if(!isset($_SESSION["Username"]))
		{	
			header("Location:index.php");
		}

        include_once('class_sanpham.php');
		include_once('components/DanhMucSanPham/class_DanhMucSanPham.php');
		$eb = new class_sanpham();
		$leb = new class_DanhMucSanPham();
		$lp = $leb->GetAllByLevel1();

		
		
		$TenSanPham = 'mac dinh';		
		$MaDanhMuc = 1;	
		$MaNguoiBan = $_SESSION["MaUser"];
		$GiaSanPham = 1;
		$MaDonViTinh = 1;
		$SoLuong = 1;
		$NhaSanXuat = 'Chua Biet';
		$HinhAnh = 'nopic.jpg';	
		//$Mota = '';
		$Mota = 'mac dinh';
		$DiemDanhGia = 1;
		//$NgayDang = getdate();
		$NgayDang = date("d-m-Y");
		//echo $NgayDang;
		$PageView = 0;
		$TinhTrangDang = 1;
		$TinhTrangXoa = 0;
		$HaveTag = 0;
		$HaveDacDiem = 0;
		
		$smg_img = '';
		$smg_cnt = '' ;
		$smg = '' ;
		$errors=0;		
		if(isset($_POST['flag']) && $_POST['flag'] == 'true' && $errors==0 ) // xac nhan da nhan nut update
		{
//			echo 'vao xu ly cap nhat';
			

			$image=$_FILES['image']['name'];
			if ($image) 
			{
				$filename = stripslashes($_FILES['image']['name']);
				$extension = getExtension($filename);
				$extension = strtolower($extension);
			
				if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
				{
					$smg_img ='Chỉ hổ trợ các file : *.jpg,*.jpeg,*.png,*.gif!';
					$errors=1;
				}
				else
				{
					$size2=filesize($_FILES['image']['tmp_name']);
		
					if ($size2 > MAX_SIZE*1024)
					{
						$smg_img ='Kích thước quá lớn (size file < 4MB)!';
						$errors=1;
					}
		
					move_uploaded_file($_FILES["image"]["tmp_name"],"../images/sanpham/" . $_FILES["image"]["name"]);
					$hinhanh = $_FILES['image']['name'] ;
					//echo 'Stored in: ' . '../images/sanpham/' . $_FILES['image']['name'];
				}
			}
			else
			{
					$errors = 1 ;
					$smg_img = 'Lỗi khi upload file hình ảnh !';
			}
			
			
			if(!$errors) 
			{
				//echo "<h1>File Uploaded Successfully! Try again!</h1>";
				if(isset($_POST['TenSanPham']) && $_POST['TenSanPham'] != '' )
				{
					$TenSanPham = $_POST['TenSanPham'];
				}
				if(isset($_POST['TenDanhMuc']) && $_POST['TenDanhMuc'] != '' )
				{
					$MaDanhMuc = $_POST['TenDanhMuc'];
				}
				if(isset($_POST['MaNguoiBan']) && $_POST['MaNguoiBan'] != 0 )
				{
					$MaNguoiBan = $_POST['MaNguoiBan'];
				}
				if(isset($_POST['GiaSanPham']) && $_POST['GiaSanPham'] != 0 )
				{
					$GiaSanPham = $_POST['GiaSanPham'];
				}
				if(isset($_POST['TenDonViTinh']) && $_POST['TenDonViTinh'] != '' )
				{
					$newclass1 = new class_DanhMucSanPham();
					$kq1 = $newclass1->GetMaDonViTinhByName($_POST['TenDonViTinh']);
					$MaDonViTinh = $kq1["MaSo"];
				}
				if(isset($_POST['SoLuong']) && $_POST['SoLuong'] != 0 )
				{
					$SoLuong = $_POST['SoLuong'];
				}
				if(isset($_POST['NhaSanXuat']) && $_POST['NhaSanXuat'] != '' )
				{
					$NhaSanXuat = $_POST['NhaSanXuat'];
				}
				if(isset($_POST['HinhAnh']) && $_POST['HinhAnh'] != '' )
				{
					$HinhAnh = $_POST['HinhAnh'];
				}
				if(isset($_POST['Mota']) && $_POST['Mota'] != '' )
				{
					$Mota = $_POST['Mota'];
				}
				if(isset($_POST['DiemDanhGia']) && $_POST['DiemDanhGia'] != 0 )
				{
					$DiemDanhGia = $_POST['DiemDanhGia'];
				}
				if(isset($_POST['NgayDang']) && $_POST['NgayDang'] != '' )
				{
					$NgayDang = $_POST['NgayDang'];
				}
				if(isset($_POST['PageView']) && $_POST['PageView'] != 0 )
				{
					$PageView = $_POST['PageView'];
				}
				
				if(isset($_POST['HaveTag']) && $_POST['HaveTag'] != 0 )
				{
					$HaveTag = $_POST['HaveTag'];
				}
				if(isset($_POST['HaveDacDiem']) && $_POST['HaveDacDiem'] != 0 )
				{
					$HaveDacDiem = $_POST['HaveDacDiem'];
				}
				
	
				$kq = $eb->DangSanPham($TenSanPham,$MaDanhMuc,$MaNguoiBan,$GiaSanPham,$MaDonViTinh,$SoLuong,$NhaSanXuat,$hinhanh,$Mota,$DiemDanhGia,$NgayDang,$PageView,$TinhTrangDang,$TinhTrangXoa,$HaveTag,$HaveDacDiem);
				if($kq != null)
				{
					$smg = '<center><h2>Thêm sản phẩm thành công !</h2>';
					$smg .= '<img src="../images/loading.gif" />';				
					$smg .= '<meta http-equiv="refresh" content="20;url=index.php?com=SanPham&fun=DanhSachSanPham" /></center>';
				}
				else
					$smg = 'Thêm Sản Phẩm thất bại !';
			}
			else
			{
					$smg = '<center><h2>Thêm sản phẩm thành công !</h2></center>';
					
			}
		}
//		else if((!isset($_POST['flag'])) || (isset($_POST['flag']) && $_POST['flag'] == 'true' && $errors==1))
		{
			$ctr_p = '' ;
			foreach($lp as $p)
			{
				$ctr_p .= '<option value="'.$p['MaDanhMuc'].'" >'.$p['TenDanhMuc'].'</option>' ;
			}
			$dsdvt = new class_sanpham();
			$ldvt = $dsdvt->ListDVT();
			$str_dvt = '';
			foreach($ldvt as $q)
			{
				$str_dvt .= '<option value="'.$q['TenDonViTinh'].'" >'.$q['TenDonViTinh'].'</option>' ;
			}
			
			echo '<form method="post" name="form1" enctype="multipart/form-data" action="index.php?com=SanPham&fun=add">
						<input type="hidden" id="flag" name="flag" value="true"/>
						<table width="745px" border="0" cellpadding="0px" cellspacing="0px">
						  <tr>
							<td colspan="2" align="center" bgcolor="#D7FCD4"><h1>  Đăng Sản Phẩm Mới</h1></td>
						 </tr>
  						  <tr>
							<td colspan="2" align="center" >'.$smg.'</td>
						  </tr>
						  <tr>
							<td height="528" colspan="2" >
							
							<table width="90%" height="525" border="0" align="center">
								<tr>
								  <td colspan="2">&nbsp;</td>
								</tr>
								<tr>
								  <td align="right" bgcolor="#CCCCCC"><strong>Tên sản phẩm : </strong></td>
								  <td bgcolor="#CCCCCC"><input name="TenSanPham" type="text" id="TenSanPham" size="60"  /></td>
								</tr>
								<tr>
								  <td align="right" bgcolor="#CCCCCC"><strong>Nhà sản xuất : </strong></td>
								  <td bgcolor="#CCCCCC"><input name="NhaSanXuat" type="text" id="NhaSanXuat" size="60"  /></td>
								</tr>
								<tr>
								  <td align="right" bgcolor="#F2F2F2"><strong>Danh Mục : </strong></td>
								  <td bgcolor="#F2F2F2"><label>
									<select name="TenDanhMuc" id="TenDanhMuc" style="width:385px" >
									  '.$ctr_p.'
									</select>
								  </label></td>
								</tr>
								<tr>
								  <td align="right" bgcolor="#F2F2F2"><strong>Giá tiền :</strong></td>
								  <td bgcolor="#F2F2F2"><input name="GiaSanPham" type="text" id="GiaSanPham" size="60"  /></td>
								</tr>
								<tr>
								  <td align="right" bgcolor="#F2F2F2"><strong>Đơn vị tính : </strong></td>
								  <td bgcolor="#F2F2F2"><label>
									<select name="TenDonViTinh" id="TenDonViTinh" style="width:385px" >
									  '.$str_dvt.'
									</select>
								  </label></td>
								</tr>
								<tr>
								  <td align="right" bgcolor="#CCCCCC"><strong>Số lượng : </strong></td>
								  <td bgcolor="#CCCCCC"><input name="SoLuong" type="text" id="SoLuong" size="60" /></td>
								</tr>
								
								<tr>
								  <td align="right" bgcolor="#CCCCCC"><strong>Hình Mô Tả : </strong></td>
								  <td bgcolor="#CCCCCC"><input type="file" id="image" name="image" /><div style="color:red">'.$smg_img.'</div></td>
								</tr>
								<tr>
								  <td height="70" align="right" bgcolor="#F2F2F2"><strong>Mô tả sản phẩm : </strong></td>
								  <td bgcolor="#F2F2F2"><textarea name="Mota" cols="45" rows="10" id="Mota" ></textarea><input type="hidden" id="hidemota" name="hidemota" /></td>
								</tr>
								  <td>&nbsp;</td>
								  <td>
								  <div style="float:left;padding-left:100px"><input type="submit" value="Thêm mới" width="20px" /></div><div style="float:right;padding-right:200px"><a href="index.php?com=SanPham&fun=add"><input type="reset" value="Làm lại" width="20px" /></a></div>
								  </td>
								</tr>
							</table></td>
						  </tr>
						</table>
						</form>';
		}

?>